#include "ForceFriction.h"


ForceFriction::ForceFriction(LimitedPlane* _plane, float _staticCoefficient, float _kinematicCoefficient)
{
	plane = _plane;
	u_s = _staticCoefficient;
	u_k = _kinematicCoefficient;
}


void ForceFriction::update(int delta) {
	float dt = delta/1000.0f;

	Vector3f plane_vec = Vector3f::normalize(  plane->v2 - plane->v1 );
	
	Vector3f x_vec(plane_vec.x, 0, 0);
	x_vec.normalize();

	//float angle = plane_vec.angle(x_vec); //angle of plane
	

	float cos_a = plane_vec.cosA(x_vec);
	float sin_a = sin(plane_vec.angle(x_vec));
	
	std::list<Particle*>::iterator I = particles.begin();
	while (I != particles.end())
	{
		float force = (*I)->force.length()*sin_a;

		float coefficient = u_s;
		if((*I)->velocity.length() > 0) {
			coefficient = u_k;
			(*I)->color = CL_Colorf::yellow;
			
		}

		float friction = (*I)->force.length()*cos_a*coefficient;
		
		Vector3f plane_dir = (*I)->force - ( (plane->normal*-1)*(*I)->force.length()*cos_a);
		plane_dir.normalize();

		force = max(0.0f,force-friction);
		(*I)->force = plane_dir*force;
		
		if(!plane->pointIsOnPlane((*I)->position) ) { //if particle isnt colliding anymore, remove from friction
			(*I)->color = CL_Colorf::aliceblue;
			I = particles.erase(I);
		} else {
			++I;
		}
	}
}
